package com.fxsh168.server.otherviews.banner;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.trace.LBSTraceClient;
import com.baidu.trace.LocationMode;
import com.baidu.trace.Trace;
import com.fxsh168.server.R;
import com.fxsh168.server.interfaces.Constant;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import java.lang.ref.WeakReference;

public class MyApplication extends Application {
    private static MyApplication mInstance = null;

    //       以下是鹰眼服务的
    private Context mContext = null;
    private Trace trace = null;   //轨迹服务
    private LBSTraceClient client = null;  // * 轨迹服务客户端
    private int serviceId = 138420;   // * 鹰眼服务ID，开发者创建的鹰眼服务对应的服务ID
    private String entityName = "";   //* entity标识
    /**
     * 轨迹服务类型（0 : 不建立socket长连接， 1 : 建立socket长连接但不上传位置数据，2 : 建立socket长连接并上传位置数据）
     */
    private int traceType = 2;

    private MapView bmapView = null;

    private BaiduMap mBaiduMap = null;

    private TrackHandler mHandler = null;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        initImageLoader();

//        以下是鹰眼服务的
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        entityName = sp.getString("mPhone", "");
        mContext = getApplicationContext();
        SDKInitializer.initialize(this);
        // 初始化轨迹服务客户端
        client = new LBSTraceClient(mContext);
        // 初始化轨迹服务
        trace = new Trace(mContext, serviceId, entityName, traceType);
        // 设置定位模式
        client.setLocationMode(LocationMode.High_Accuracy);
        mHandler = new TrackHandler(this);
    }

    /**
     * 初始化UIL
     */
    public void initImageLoader() {
        DisplayImageOptions.Builder options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)// 内存缓存
                .cacheOnDisk(true)// 磁盘缓存
                .showImageOnLoading(R.drawable.ic_launcher)//加载中显示的图片
                .showImageOnFail(R.drawable.ic_launcher)//加载失败显示的图片
                .considerExifParams(true)// 是否考虑EXIF信息，比如拍照方向
                .displayer(new FadeInBitmapDisplayer(300));//淡入动画
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(getApplicationContext());
        // 取消缓存多张尺寸不同的同一张图片
        config.denyCacheImageMultipleSizesInMemory();
        // 设置显示选项
        config.defaultDisplayImageOptions(options.build());
        // 生成缓存文件的生成器，保证唯一的文件名，可以不设置，默认使用hash算法，也是可以保证不重名的
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        // 磁盘缓存大小
        config.diskCacheSize(100 * 1024 * 1024); // 100 MB
        // 内存缓存大小
        config.memoryCacheSize((int) (Runtime.getRuntime().freeMemory() / 4));
        // 任务处理顺序，默认是FIFO 先进先出， LIFO 后进先出
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        // 打印调试日志
//		config.writeDebugLogs(); // Remove for release app
        ImageLoader.getInstance().init(config.build());
    }


    //    以下是鹰眼服务的
    static class TrackHandler extends Handler {
        WeakReference<MyApplication> trackApp;

        TrackHandler(MyApplication trackApplication) {
            trackApp = new WeakReference<MyApplication>(trackApplication);
        }

        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(trackApp.get().mContext, (String) msg.obj, Toast.LENGTH_SHORT).show();
        }
    }

    public Context getmContext() {
        return mContext;
    }

    public Trace getTrace() {
        return trace;
    }

    public LBSTraceClient getClient() {
        return client;
    }

    public int getServiceId() {
        return serviceId;
    }

    public String getEntityName() {
        return entityName;
    }

    public Handler getmHandler() {
        return mHandler;
    }

    public MapView getBmapView() {
        return bmapView;
    }

    public BaiduMap getmBaiduMap() {
        return mBaiduMap;
    }


}
